package com.gelpag.base.init;

import been.RoleInfo;
import com.alibaba.fastjson.JSON;
import com.gelpag.base.business.menu.MenuMapper;
import com.gelpag.base.business.role.RoleMapper;
import entity.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 继承ApplicationRunner接口后项目启动时会按照执行顺序执行run方法
 * 1表示最先执行
 */
@Component
@Order(value = 1)
public class InitRedisService implements ApplicationRunner {

    Logger logger = LoggerFactory.getLogger(InitRedisService.class);

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Resource
    RoleMapper roleMapper;

    @Resource
    MenuMapper menuMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        logger.info("=========== 项目启动后，初始化 Redis =============");
        this.initRedis(stringRedisTemplate);
        logger.info("=========== 项目启动后，初始化 Redis 结束 =============");
    }

    public void initRedis(StringRedisTemplate stringRedisTemplate) {

        //删除不验证权限的url
        stringRedisTemplate.delete(Const.REDIS_HEAD_BASE + "_" + Const.REDIS_NO_CHECK_URL);

        //删除验证权限的url
        Set<String> keys = stringRedisTemplate.keys(Const.REDIS_HEAD_BASE + "_" + Const.REDIS_CHECK_URL);
        if (!CollectionUtils.isEmpty(keys)) {
            stringRedisTemplate.delete(keys);
        }

        //删除角色id对应的RoleInfo
        Set<String> keys1 = stringRedisTemplate.keys(Const.REDIS_HEAD_BASE + "_" + Const.REDIS_ROLE_ID + "*");
        if (!CollectionUtils.isEmpty(keys1)) {
            stringRedisTemplate.delete(keys1);
        }

        //初始化不验证权限的url
        List<String> noCheckUrl = menuMapper.getNoCheckUrl();
        //将list集合转化为字符串
        String noCheckUrlStr = JSON.toJSONString(noCheckUrl);
        if (noCheckUrlStr != null) {
            //证明有不需要验证权限的url
            stringRedisTemplate.opsForValue().set(Const.REDIS_HEAD_BASE+"_"+Const.REDIS_NO_CHECK_URL, noCheckUrlStr);
        }

        List<RoleInfo> roleList = roleMapper.getRoleList();
        for(RoleInfo roleInfo: roleList) {
            //刷新redis中的角色对应的url
            List<String> roleUrls = roleMapper.getResourceUrlsByRoleId(roleInfo.getRole_id());
            //将list集合转换为字符串
            String roleUrlStr = JSON.toJSONString(roleUrls);
            if (roleUrlStr != null) {
                stringRedisTemplate.opsForValue().set(Const.REDIS_HEAD_BASE+"_"+Const.REDIS_CHECK_URL+"_"+roleInfo.getRole_id(),roleUrlStr);
            }

            //刷新角色的bean
            stringRedisTemplate.opsForValue().set(Const.REDIS_HEAD_BASE+"_"+Const.REDIS_ROLE_ID+"_"+roleInfo.getRole_id(), JSON.toJSONString(roleInfo));
        }

    }
}
